package com.tgra;

import com.tgra.ParticleEffect;

import com.tgra.Sphere;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL11;

public class Maze3D implements ApplicationListener {

	Camera cam;
	float cameraX; 
	float cameraZ;

	Skybox skybox;
	TransCube billboard;
	TransCube billboard_2;

	Cube floor;
	Cube solidWall;
	float carZ = 1.0f;
	float carX = 1.0f;
	float carY = 0.0f;

	Cube turnBox;
	
	MovingCube car;
	MovingCamera mCam;

	float rotationAngle = 0.0f;
	float deltaTime = 0.0f;

	Sphere sphere;
	Sphere ball;
	Sphere ball_2;
	Sphere ball_3;
	Sphere cigar;


	ParticleEffect particleEffect;
	float elapsedTime = 0;
	
	Music glass;
	Music birds;






	@Override
	public void create() {
		// music
		birds = Gdx.audio.newMusic(Gdx.files.internal("assets/14854__reinsamba__nightingale-song.wav"));
		birds.play();
		
		
		Gdx.gl11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		Gdx.gl11.glMatrixMode(GL11.GL_PROJECTION);
		Gdx.gl11.glLoadIdentity();
		Gdx.glu.gluPerspective(Gdx.gl11, 90, 1.333333f, 0.2f, 30.0f);
		Gdx.gl11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

		// Camera for an overview of the maze
		//cam = new Camera(new Point3D(-1.0f, 3.0f, 1.0f), new Point3D(0.0f, 3.0f, 1.0f), new Vector3D(0.0f, 1.0f, 0.0f));

		// Camera for movement, high overview
		//cam = new Camera(new Point3D(0.5f, 8.0f, 0.5f), new Point3D(0.5f, 0.3f, 2.0f), new Vector3D(0.0f, 1.0f, 0.0f));
		
		mCam = new MovingCamera(new Point3D(-1.0f, 2.0f, -1.0f), new Point3D(0.0f, 1.5f, 0.5f), new Vector3D(0.0f, 1.0f, 0.0f));


		floor = new Cube("assets/gras.png");
		skybox = new Skybox("assets/lightning.png");
		car = new MovingCube();
		sphere = new Sphere(50, 24, "assets/star01.bmp");
		ball = new Sphere(50, 24, "assets/gold.png");
		ball_2 = new Sphere(50, 24, "assets/red.png");
		ball_3 = new Sphere(50, 24, "assets/green.png");
		turnBox = new Cube("assets/waves.png");
		this.particleEffect = new ParticleEffect();
		billboard = new TransCube("assets/maya.png");
		billboard_2 = new TransCube("assets/forest.png");
		cigar = new Sphere(50, 24, "assets/ocean.png");
		solidWall = new Cube("assets/first_texture.jpg");

	}

	private void update()
	{
		float deltaTime = Gdx.graphics.getDeltaTime();
		elapsedTime += deltaTime;

		this.deltaTime = deltaTime;
		rotationAngle += 10.0f * deltaTime;
		
		this.particleEffect.update(deltaTime);
		ball.update(deltaTime, 15.0f);
		ball_2.update(deltaTime, 7.0f);
		ball_3.update(deltaTime, 10.0f);
		
		car.update(elapsedTime);
		mCam.updateCameraPos(elapsedTime);
		mCam.cameraLookAt(elapsedTime);

	}

	private void collition()
	{
		
		int cX = (int)car.position.x;
		int cZ = (int)car.position.z;

		if(cX == 4.0f &&cZ== 4.0f)
		{
			Gdx.gl11.glPushMatrix();
			Gdx.gl11.glTranslatef(3.99f, 2.0f, 4.0f);
			Gdx.gl11.glScalef(0.0f, 1.0f, 1.0f);
			solidWall.draw();
			Gdx.gl11.glPopMatrix();	 
			
		}

	}

	private void drawBaseFloor()
	{
		for(float fx = -1.0f; fx < 11.0f; fx += 1.0)		
		{			
			for(float fz = -1.0f; fz < 11.0f; fz += 1.0)			
			{				
				Gdx.gl11.glPushMatrix();				
				Gdx.gl11.glTranslatef(fx, 1.0f, fz);
				Gdx.gl11.glScalef(1.0f, 1.0f, 1.0f);
				floor.draw();				
				Gdx.gl11.glPopMatrix();			
			}		
		}	
	}



	private void display()
	{
		Gdx.gl11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
		Gdx.gl11.glDisable(GL11.GL_LIGHTING);
		Gdx.gl11.glDisable(GL11.GL_DEPTH_TEST);
		Gdx.gl11.glMatrixMode(GL11.GL_PROJECTION);

		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glLoadIdentity();
		Gdx.gl11.glOrthof(0, 1, 0, 1, 0, 1);

		Gdx.gl11.glMatrixMode(GL11.GL_MODELVIEW);
		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glLoadIdentity();


		Gdx.gl11.glDepthMask(false);
		skybox.draw();

		Gdx.gl11.glDepthMask(true);

		// pop model view matrix
		Gdx.gl11.glPopMatrix();

		Gdx.gl11.glMatrixMode(GL11.GL_PROJECTION);

		// pop the projection matrix.
		Gdx.gl11.glPopMatrix();

		Gdx.gl11.glMatrixMode(GL11.GL_MODELVIEW);

		// Set the ModelView matrix with respect to the camera.
		mCam.setModelViewMatrix();


		Gdx.gl11.glEnable(GL11.GL_LIGHTING);
		Gdx.gl11.glEnable(GL11.GL_LIGHT0);
		Gdx.gl11.glEnable(GL11.GL_LIGHT1);
		Gdx.gl11.glEnable(GL11.GL_DEPTH_TEST);


		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(3.0f, 5.0f, 5.0f);
		this.particleEffect.display();

		Gdx.gl11.glPopMatrix();



		float[] lightDiffuse = {1.0f, 1.0f, 1.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, lightDiffuse, 0);

		float[] lightPosition = {5.0f, 10.0f, 15.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT0, GL11.GL_POSITION, lightPosition, 0);

		float[] lightDiffuse1 = {0.5f, 0.5f, 0.5f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT1, GL11.GL_DIFFUSE, lightDiffuse1, 0);

		float[] lightPosition1 = {-5.0f, -10.0f, -15.0f, 1.0f};
		Gdx.gl11.glLightfv(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition1, 0);

		float[] materialDiffuse = {1.0f, 1.0f, 1.0f, 0.6f};
		Gdx.gl11.glMaterialfv(GL11.GL_FRONT, GL11.GL_DIFFUSE, materialDiffuse, 0);

		drawBaseFloor();

		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(10.5f, 2.8f, 4.5f);
		Gdx.gl11.glScalef(0.0f, 3.0f, 11.0f);
		this.billboard_2.draw();
		Gdx.gl11.glPopMatrix();

		car.display();

		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(3.0f, 5.0f, 5.0f);
		Gdx.gl11.glScalef(0.5f, 0.5f, 0.5f);
		Gdx.gl11.glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f);
		sphere.draw();
		Gdx.gl11.glPopMatrix();

		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(0.0f, 0.5f, 0f);
		ball.display(2.0f, 2.0f);
		ball_2.display(1.7f, 2.3f);
		ball_3.display(2.2f, 1.9f);		
		Gdx.gl11.glPopMatrix();
		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(3.0f, 1.25f, 9.0f);
		Gdx.gl11.glRotatef(rotationAngle*10.0f, 1.0f, 0.0f, 0.0f);
		float z = 9.0f;
		turnBox.display();
		for (int i = 0; i < 5; i++)
		{
			Gdx.gl11.glPopMatrix();
			Gdx.gl11.glPushMatrix();
			z -= 0.5f;
			Gdx.gl11.glTranslatef(3.0f, 1.25f, z);
			Gdx.gl11.glRotatef(rotationAngle*10.0f, 1.0f, 0.0f, 0.0f);
			turnBox.display();			
		}
		Gdx.gl11.glPopMatrix();


		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(4.0f, 2.0f, 4.0f);
		Gdx.gl11.glScalef(0.0f, 1.0f, 1.0f);
		this.billboard.draw();
		Gdx.gl11.glPopMatrix();	

		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(7.0f, 1.25f, 4.0f);
		Gdx.gl11.glScalef(0.8f, 0.7f, 4.0f);
		Gdx.gl11.glRotatef(-rotationAngle, -1.0f, 0.0f, .0f);
		cigar.cigarDisplay();
		Gdx.gl11.glPopMatrix();

		collition();

	}


	@Override
	public void dispose() {
		birds.dispose();

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {

		update();

		display();


	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}
